/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.security.data;

import com.telsoft.security.entity.SEC_ETT_Right;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author nguyenthang
 */
public class SEC_DATA_Right {
    private Connection connect = null;
    public void addRight(SEC_ETT_Right right){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        try {
            connect = connectDB.dbConnect();
            String query = "INSERT INTO SEC_RIGHT(RIGHT_ID, RIGHT_NAME, RIGHT_CODE) VALUES (?,?,?)";
            stmt = connect.prepareStatement(query);
            stmt.setInt(1, right.getRIGHT_ID());
            stmt.setString(2, right.getRIGHT_NAME());
            stmt.setString(3, right.getRIGHT_CODE());
            stmt.execute();
            connectDB.dbClose(connect, stmt, null);
        } catch (Exception ex){
            ex.toString();
        }
    }
    
    public SEC_ETT_Right getAllByRightID(int RIGHT_ID){
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        SEC_ETT_Right returnValue = new SEC_ETT_Right();
        PreparedStatement stmt;
        ResultSet rs;
        try {
           connect = connectDB.dbConnect();
           String query = "SELECT * FROM SEC_RIGHT WHERE RIGHT_ID = ?";
           stmt = connect.prepareStatement(query);
           stmt.setInt(1, RIGHT_ID);
           rs = stmt.executeQuery();
           while (rs.next()){
               returnValue.setRIGHT_ID(rs.getInt("RIGHT_ID"));
               returnValue.setRIGHT_NAME(rs.getString("RIGHT_NAME"));
               returnValue.setRIGHT_CODE(rs.getString("RIGHT_CODE"));
           }
           connectDB.dbClose(connect, stmt, rs);
        } catch (Exception ex){
            ex.toString();
        }
        return returnValue;
    }
    
    public List<SEC_ETT_Right> getAllRight(){
        List<SEC_ETT_Right> listRight = new ArrayList<SEC_ETT_Right>();
        SEC_ETT_Right right;
        SEC_DAOHelper connectDB = new SEC_DAOHelper();
        PreparedStatement stmt;
        ResultSet rs;
        try {
            connect = connectDB.dbConnect();
            String query = "SELECT * FROM SEC_RIGHT ORDER BY RIGHT_ID ASC";
            stmt = connect.prepareStatement(query);
            rs = stmt.executeQuery();
            while (rs.next()){
                right = new SEC_ETT_Right();
                right.setRIGHT_ID(rs.getInt("RIGHT_ID"));
                right.setRIGHT_CODE(rs.getString("RIGHT_CODE"));
                right.setRIGHT_NAME(rs.getString("RIGHT_NAME"));
                listRight.add(right);
            }
            connectDB.dbClose(connect, stmt, rs);
        } catch (Exception ex){
            ex.toString();
        }
        return listRight;
    }
}
